package com.hs.http;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import com.hs.SimpleLotteryInfo.R;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class HttpConn
{
	public static final int HTTP_EVT_OK 			= 9999;
	public static final int HTTP_EVT_FAIL 			= 9998;
	public static final int HTTP_EVT_EXCEPTION		= 9997;
	
	private Handler m_handler;
	private Context m_cxt;
	
	public static final String BUNDLE_KEY_DATA		= "data";
	public static final String BUNDLE_KEY_ARRAYLIST = "arrlist";
	
	public HttpConn( Context _cxt , Handler _handler )
	{
		m_cxt = _cxt;
		m_handler = _handler;
	}
	
	public void getData( final String _strAddress )
	{
		new Thread( new Runnable()
		{
			public void run()
			{
				StringBuilder strB = new StringBuilder();
				
				try
				{
					URL u = new URL(_strAddress);
					HttpURLConnection conn = (HttpURLConnection)u.openConnection();
					conn.setConnectTimeout(10000);
					conn.setUseCaches(false);
					
					if( conn.getResponseCode() == HttpURLConnection.HTTP_OK )
					{
						BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() , "EUC-KR"));
						
						while(true)
						{
							String strLine = br.readLine();
							
							if( strLine == null )
							{
								break;
							} // end if( strLine == null )
							
							strB.append(strLine + "\n");
							
						} // end while(true)
						
					} // end if( conn.getResponseCode() == HttpURLConnection.HTTP_OK )
					
					else
					{
						switch(conn.getResponseCode())
						{
							case HttpURLConnection.HTTP_ACCEPTED:
							{
								//Error code 202
								break;
							}
							case HttpURLConnection.HTTP_BAD_GATEWAY:
							{
								//Error code 502
								break;
							}
							case HttpURLConnection.HTTP_BAD_METHOD:
							{
								//Error code 40
								break;
							}
							
							case HttpURLConnection.HTTP_BAD_REQUEST:
							{
								//Error code 400;
								break;
							}
							case HttpURLConnection.HTTP_CLIENT_TIMEOUT:
							{
								//Error code 408;
								break;
							}
							case HttpURLConnection.HTTP_CONFLICT:
							{
								//Error code 409;
								break;
							}
							case HttpURLConnection.HTTP_CREATED:
							{
								//Error code 201;
								break;
							}
							case HttpURLConnection.HTTP_ENTITY_TOO_LARGE:
							{
								//Error code 413;
								break;
							}
							case HttpURLConnection.HTTP_FORBIDDEN:
							{
								//Error code 403;
								break;
							}
							case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
							{
								//Error code 504;
								break;
							}
							case HttpURLConnection.HTTP_GONE:
							{
								//Error code 410;
								break;
							}
							case HttpURLConnection.HTTP_LENGTH_REQUIRED:
							{
								//Error code 411;
								break;
							}
							case HttpURLConnection.HTTP_MOVED_PERM:
							{
								//=	301;
								break;
							}
							case HttpURLConnection.HTTP_MOVED_TEMP:
							{
								//Error code 302;
								break;
							}
							case HttpURLConnection.HTTP_MULT_CHOICE:
							{
								//Error code 300;
								break;
							}
							case HttpURLConnection.HTTP_NO_CONTENT:
							{
								//Error code 204;
								break;
							}
							case HttpURLConnection.HTTP_NOT_ACCEPTABLE:
							{
								//Error code 406;
								break;
							}
							case HttpURLConnection.HTTP_NOT_AUTHORITATIVE:
							{
								//Error code 203;
								break;
							}
							case HttpURLConnection.HTTP_NOT_FOUND:
							{
								//Error code 404;
								break;
							}
							case HttpURLConnection.HTTP_NOT_IMPLEMENTED:
							{
								//Error code 501;
								break;
							}
							case HttpURLConnection.HTTP_NOT_MODIFIED:
							{
								//Error code 304;
								break;
							}
							case HttpURLConnection.HTTP_OK:
							{
								//Error code 200;
								break;
							}
							case HttpURLConnection.HTTP_PARTIAL:
							{
								//Error code 206;
								break;
							}
							case HttpURLConnection.HTTP_PAYMENT_REQUIRED:
							{
								//Error code 402;
								break;
							}
							case HttpURLConnection.HTTP_PRECON_FAILED:
							{
								//Error code 412;
								break;
							}
							case HttpURLConnection.HTTP_PROXY_AUTH:
							{
								//Error code 407;
								break;
							}
							case HttpURLConnection.HTTP_REQ_TOO_LONG:
							{
								//Error code 414;
								break;
							}
							case HttpURLConnection.HTTP_RESET:
							{
								//Error code 205;
								break;
							}
							case HttpURLConnection.HTTP_SEE_OTHER:
							{
								//Error code 303;
								break;
							}
							case HttpURLConnection.HTTP_SERVER_ERROR:
							{
								// HttpURLConnection.HTTP_INTERNAL_ERROR:
								//Error code 500;
								break;
							}
							case HttpURLConnection.HTTP_USE_PROXY:
							{
								//Error code 305;
								break;
							}
							case HttpURLConnection.HTTP_UNAUTHORIZED:
							{
								//Error code 401;
								break;
							}
							case HttpURLConnection.HTTP_UNSUPPORTED_TYPE:
							{
								//Error code 415;
								break;
							}
							case HttpURLConnection.HTTP_UNAVAILABLE:
							{
								//Error code 503;
								break;
							}
							case HttpURLConnection.HTTP_VERSION:
							{
								//Error code 505;
								break;
							}
							
						} // end switch(conn.getResponseCode())
						
						m_handler.sendMessage(m_handler.obtainMessage(HTTP_EVT_FAIL, m_cxt.getString(R.string.str_network_err) + conn.getResponseCode()));
					} // end else
					
					conn.disconnect();
					
					if( strB.length() > 0 )
					{
						m_handler.sendMessage(m_handler.obtainMessage(HTTP_EVT_OK, strB.toString()));
					} // end if( strB.length() > 0 )
					
				} // end try
				
				catch(Exception e)
				{
					m_handler.sendMessage(m_handler.obtainMessage(HTTP_EVT_EXCEPTION, m_cxt.getString(R.string.str_network_exception)));
					e.printStackTrace();
				} // end catch(Exception e)
			}
		}).start(); 
	}
}
